package com.gvitz.transaction.utils;

import java.util.ArrayList;

import com.gvitz.transaction.R;
import com.gvitz.transaction.database.gvitzDB;
import com.gvitz.transaction.models.Card;
import com.gvitz.transaction.models.transaction;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


/** Clase Adapter para ver la lista de los transactions
 * @author Orlando
 *
 */
public class TransItemAdapter extends ArrayAdapter<transaction> {
	private ArrayList<transaction> transactions;
	private Activity act;

	public TransItemAdapter(Context context, int textViewResourceId, ArrayList<transaction> mtransactions,Activity activity) {
		super(context, textViewResourceId, mtransactions);
		this.transactions = mtransactions;
		this.act = activity;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.transaction_item, null);
		}
		
		transaction trans = transactions.get(position);
		
		if (trans  != null) {
			
			TextView titulo = (TextView) v.findViewById(R.id.titulo_item_transaccion);
			TextView monto = (TextView) v.findViewById(R.id.tipo_item_monto);
			TextView fecha  = (TextView) v.findViewById(R.id.tipo_item_fecha);
			TextView digits = (TextView) v.findViewById(R.id.tarjeta_item_transaccion);
			
			gvitzDB dbHelper=  new gvitzDB(act.getApplicationContext());
			Card t = dbHelper.GetTarjeta(trans.getId_tarjeta());

			if (titulo != null) {
				titulo.setText(trans.getNombre_cliente());
			}
						
			if (monto != null) {
				 monto.setText("$"+Double.toString(trans.getMonto()));
			}
			
			if (fecha!= null) {
				 fecha.setText(trans.getFecha());
			}
			
			if (digits != null) {
				digits.setText("**** **** **** " + t.getCard_number());
			}


		}
		return v;
	}
}
